/**
 * 
 */
package br.ufpe.cin.mac.demo.ImageProcesser.client;

import java.net.InetAddress;

import br.ufpe.cin.mac.demo.ImageProcesser.client.gui.ImageClientGUI;
import br.ufpe.cin.mac.demo.ImageProcesser.server.IImageProServer;
import br.ufpe.cin.mac.demo.ImageProcesser.server.ImageProServer;
import br.ufpe.cin.mac.middleware.Middleware;
import br.ufpe.cin.mac.util.Proxy;

/**
 * @author root
 *
 */
public class ImageProClient {
	
	private String server;
	private String imagePath;

	
	/**
	 * @return the imagePath
	 */
	public String getImagePath() {
		return imagePath;
	}

	/**
	 * @param imagePath the imagePath to set
	 */
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	/**
	 * @return the server
	 */
	public String getServer() {
		return server;
	}

	/**
	 * @param server the server to set
	 */
	public void setServer(String server) {
		this.server = server;
	}

	/**
	 * 
	 */
	public ImageProClient() {
		// TODO Auto-generated constructor stub
	}
	
	public void startClient(IImageProServer proxy){
		new ImageClientGUI(imagePath, proxy);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ImageProClient client = new ImageProClient();

		for (int i = 0; i < args.length; i++) {

			if (args[i].toLowerCase().equals("-h")) {
				System.out.println("Usage: java JAINetworkDemo -server "
						+ "ImageServer -imagePath " + "ImageDirectoryOnServer");
				System.out.println("If no serverName is provided using -server"
						+ ", the localhost will be used as the server"
						+ " by default.");

				System.out.println("If no image path is provided using "
						+ "-imagePath, the default path of "
						+ "../sample/images will be used.");
				System.exit(0);

			} else if ("-server".equalsIgnoreCase(args[i])) {
				client.setServer(args[++i]);
			} else if ("-imagePath".equalsIgnoreCase(args[i])) {
				client.setImagePath(args[++i]);
			}
		}

		if (client.getServer() == null) {
			try {
				client.setServer(InetAddress.getLocalHost().getHostAddress());
			} catch (java.net.UnknownHostException ukhe) {
				throw new RuntimeException("Unknown local host.\n"
						+ ukhe.getMessage());
			}
		}

		String sep = System.getProperty("file.separator");
		if (client.getImagePath() == null) {
			client.setImagePath( "src" + sep + "br" + sep + "ufpe" + sep + "cin" + sep + "mac"
					+ sep + "demo" + sep + "ImageProcesser" + sep + "server"
					+ sep);
		}

		Middleware mid = new Middleware(client.getServer() , "Config.xml");
		//Middleware mid = new Middleware("localhost", "Config.xml");

		Proxy m = new Proxy(client, ImageProServer.class, mid);

		IImageProServer proxy = (IImageProServer) java.lang.reflect.Proxy
				.newProxyInstance(Thread.currentThread()
						.getContextClassLoader(),
						new Class[] { IImageProServer.class }, (Proxy) m);
		client.startClient(proxy);

	}

}
